<template>
  <wd-table :data="dataList" custom-class="p-4 text-center" :height="400">
    <wd-table-col prop="date" align="center" width="45%" label="日期" />
    <wd-table-col prop="percent" width="45%" align="center" label="执行度" />
  </wd-table>
</template>

<script setup lang="ts">
import { getSheetAllData } from '@/api/daily';
import { ResultCodeEnum } from '@/enums';
import { isString } from '@/utils/common';
import { formatDate, getStartOrEndDate } from '@/utils/date';
import dayjs from 'dayjs';

type TDataItem = {
  date: string;
  percent: number | string;
};

const dataList = ref<TDataItem[]>([]);

const getCurrentWeekDates = (): string[] => {
  const { start } = getStartOrEndDate('week'); // 获取当前周的第一天（周一）
  const dates = [];
  for (let i = 0; i < 7; i++) {
    dates.push(dayjs(start).add(i, 'day').format('YYYY-MM-DD')); // 按天递增
  }
  return dates;
};

const weekDateList = getCurrentWeekDates();

const getData = async () => {
  dataList.value = [];
  const res = await getSheetAllData();
  if (res.code === ResultCodeEnum.SUCCESS) {
    const { sheetData, dateRowIndex, nameColIndex } = res.data;
    sheetData.forEach((row: any) => {
      if (weekDateList.includes(formatDate(row[0]))) {
        dataList.value.push({
          date: formatDate(row[0]),
          percent: isString(row[nameColIndex])
            ? row[nameColIndex]
            : row[nameColIndex] * 100 + '%'
        });
      }
    });
  }
};

getData();
</script>

<style lang="scss" scoped></style>
